<?php

/**
 * Class ApiService
 */
class InformationService extends BaseService
{
    private static $self = NULL;

    /**
     * @return InformationService|null
     */
    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }

    /**
     * @var null
     */
    private $mInformation           = NULL;

    /**
     * 实例化model
     */
    public function __construct(){

        $this->mInformation          = new Involved_InformationModel();

    }

    /**
     * 参数验证
     * @param $obj
     * @param $arr
     * @author alice
     * @date 2016-1-19
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if(empty($arr)) return true;
        $obj    =   (array) $obj;

        foreach($arr as $v){
            if(empty($obj[$v])){
                return false;}
        }
        return true;
    }

    /**
     * 资讯列表
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowInformationList()
    {
        return $this->mInformation->ShowInformationList();

    }

    /**
     * 资讯详情
     * @param $id
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowInformationInfo($id)
    {
        return $this->mInformation->ShowInformationInfo($id);

    }

    /**
     * 新增资讯
     * @param $paramObj
     * @author alice
     * @date 2016-1-19
     * @return int
     * @throws Exception
     */
    public function AddInformation($paramObj)
    {
        #参数验证
        $check = $this->checkProperties($paramObj, ['title']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #上传图片
        if(!empty($paramObj->img))
        {
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(343, 134);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;
        }

        #添加资讯信息
        $aData = [
            'title'             => $paramObj->title,
            'author'            => $paramObj->author,
            'source'            => $paramObj->source,
            'cate_id'           => $paramObj->cate_id,
            'keywords'          => $paramObj->keywords,
            'introduces'        => $paramObj->introduces,
            'content'           => $paramObj->content,
            'created'           => time(),
        ];

        return $this->mInformation->insert($aData);
    }

    /**
     * 修改资讯
     * @param $paramObj
     * @author alice
     * @date 2016-1-19
     * @return bool
     */
    public function EditInformation($paramObj)
    {
        #上传图片
        if(!empty($paramObj->img))
        {
            $file = $paramObj->img;
            $clientName = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $newName = md5(date('ymdhis') . $clientName) . "." . $extension;
            $file->move(public_path() . '/resource/upload', $newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(343, 134);
            $image = file_get_contents(public_path() . '/resource/upload/' . $newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path() . '/resource/upload/thumb_' . $newName);

            $aData['img'] = '/resource/upload/' . $newName;
        }

        #添加资讯信息
        $aData = [
            'title'             => $paramObj->title,
            'author'            => $paramObj->author,
            'source'            => $paramObj->source,
            'cate_id'           => $paramObj->cate_id,
            'keywords'          => $paramObj->keywords,
            'introduces'        => $paramObj->introduces,
            'content'           => $paramObj->content,
            'created'           => time(),
        ];

        return $this->mInformation->update($aData,$paramObj->zx_id);
    }



}